/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package systole.domain.persons;

import java.util.Calendar;
import java.util.Date;

/**
 *
 * @author jmj
 */
public class AgeUtils {

    /**
     * 
     */
    public AgeUtils() {
        super();
    }

    /**
     * @param birthdate
     * @return Age value form bithdate
     */
    public int getAgeFromBirthDate(Date birthdate) {
        if (birthdate != null) {
            Calendar calDOB = Calendar.getInstance();
            calDOB.setTime(birthdate);
            Calendar today = Calendar.getInstance();
            int age = today.get(Calendar.YEAR) - calDOB.get(Calendar.YEAR);
            if (today.get(Calendar.MONTH) < calDOB.get(Calendar.MONTH)
                    || ((today.get(Calendar.MONTH) == calDOB.get(Calendar.MONTH)
                        && (today.get(Calendar.DAY_OF_MONTH) < calDOB.get(Calendar.DAY_OF_MONTH))))) {
                age--;
            }
            return age;
        }
        return 0;
    }

    /**
     * @param birthdate
     * @return decade value from birthdate
     */
    public int getDecadeFromBirthDate(Date birthdate) {
        int age = this.getAgeFromBirthDate(birthdate);
        return (age / 10) +1;
    }

    /**
     * @param age
     * @return birthdate from age
     */
    public Date getBirthDateFromAge(int age) {
        Calendar calDOB = Calendar.getInstance();
        calDOB.add(Calendar.YEAR, age * -1);
        return calDOB.getTime();
    }
    
}
